/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/mfc/PGLStatic.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPGLStatic

CPGLStatic::CPGLStatic(CPGLRegionPtr pRegion, DWORD dwFlags)
: m_pRegion(pRegion), m_GDIpHelper(&m_GDIpInterface, m_pRegion),
m_dwFlags(dwFlags)
{
	if (m_dwFlags & OptionTripleBuffering )
		SetTripleBuffering( true );
}

CPGLStatic::~CPGLStatic()
{
	m_pRegion.reset();
}


BEGIN_MESSAGE_MAP(CPGLStatic, CStatic)
	//{{AFX_MSG_MAP(CPGLStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_DESTROY()
	ON_WM_RBUTTONDOWN()
	ON_WM_NCHITTEST()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPGLStatic message handlers

void CPGLStatic::OnPaint() 
{
	Draw();
}

BOOL CPGLStatic::OnEraseBkgnd(CDC* pDC) 
{
    if (m_pRegion)	
        return TRUE;
    else
        return CStatic::OnEraseBkgnd(pDC);
}

void CPGLStatic::OnDestroy() 
{
	CStatic::OnDestroy();
	
	m_GDIpHelper.Destroy();
	
	m_pRegion.reset();
}


void CPGLStatic::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CPGLAllPropsDlg propDlg;

	propDlg.SetRegion(m_pRegion);
	propDlg.DoModal();
	// redraw dialog
	SetDirty();
	
	CStatic::OnRButtonDown(nFlags, point);
}

//UINT CPGLStatic::OnNcHitTest(CPoint point)  
#if _MSC_VER >= 1400
LRESULT CPGLStatic::OnNcHitTest(CPoint point)
#else
UINT CPGLStatic::OnNcHitTest(CPoint point) // Keep compatibility with 7.1
#endif	
{
	return HTCLIENT;
}

void CPGLStatic::Draw()
{
	using namespace Gdiplus;

	CPaintDC dc(this); // device context for painting
	
	if (!m_pRegion)
		return;

try
{
	// Get client rect...
	CRect clientRect;
	GetClientRect(clientRect);
	if((clientRect.Width() <= 0) || (clientRect.Height() <= 0))
		return ; // nothing to do here....

	Graphics graphics(dc.m_hDC);
	ASSERT(graphics.GetLastStatus()==Ok);
	// Resizing
	if (clientRect.Width() != m_GDIpInterface.GetMainBBWidth() || clientRect.Height() != m_GDIpInterface.GetMainBBHeight()) 
	{
		m_GDIpInterface.SetMainBoundingBox(0,0,clientRect.Width(),clientRect.Height());
		SetDirty();
	
		m_GDIpInterface.SetGraphics(&graphics);
		m_GDIpInterface.BeginPaint(dc.m_hDC);
			m_pRegion->SetViewport(0,0,clientRect.Width(),clientRect.Height());
			if (m_dwFlags & OptionNoZoomAll)
				m_pRegion->RefreshView(m_GDIpInterface,true);
			else
				m_pRegion->ZoomAll(m_GDIpInterface,true);

		m_GDIpInterface.EndPaint();				
	}

	m_GDIpHelper.Draw(clientRect, dc.m_hDC, &graphics);

}
catch(std::exception const& e)
{
    AfxMessageBox(CString("Execption: ")+e.what());
}

}


void CPGLStatic::SetTripleBuffering(bool on)
{
	m_GDIpHelper.SetTripleBuffering(on);
	InvalidateRect(NULL,FALSE);
}

void CPGLStatic::SetDirty()
{
	m_GDIpHelper.SetDirty();
	InvalidateRect(NULL,FALSE);
};

void CPGLStatic::SetFrontDirty()
{
	m_GDIpHelper.SetFrontDirty();
	InvalidateRect(NULL,FALSE);
};

void CPGLStatic::SetCacheDirty()
{
	m_GDIpHelper.SetCacheDirty();
	InvalidateRect(NULL,FALSE);
};
